import { ref, reactive } from 'vue'
import { jobApi } from '@/api/modules/job/job'
import type { Job } from '@/api/modules/job/job'

export function useJob() {
  const filters: { [key: string]: any[] } = {
    status: [
      { text: '运行', value: '1' },
      { text: '暂停', value: '0' }
    ],
    concurrent: [
      { text: '是', value: '1' },
      { text: '否', value: '0' }
    ],
    misfirePolicy: [
      { text: '立即执行', value: '1' },
      { text: '执行一次', value: '2' },
      { text: '放弃执行', value: '3' }
    ]
  }

  const filtersText: { [key: string]: any } = {}
  for (const key in filters) {
    filtersText[key] = {}
    filters[key].forEach(item => {
      filtersText[key][item.value] = item.text
    })
  }

  const handleFormatter = (row: any, column: any, cellValue: any) => {
    return filtersText[column.property][cellValue]
  }

  const statusData = reactive({} as { [key: string]: number })
  const refTable = ref()
  const tableData = reactive({ resp: { list: [], total: 0 }, loading: false })
  const handleQuery = (page: AxiosType.Pagination) => {
    tableData.loading = true
    jobApi.findListPage(page).then(resp => {
      if (resp.success) {
        tableData.resp = resp.data
        resp.data.list.forEach((j: Job) => {
          statusData[j.jobId] = j.status
        })
      }
      tableData.loading = false
    })
  }

  return { refTable, tableData, handleQuery, filters, filtersText, handleFormatter, statusData }
}
